package com.hwg.emp.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.common.dto.KeysQueryDto;
import com.hwg.emp.common.util.ThisAppUtil;
import com.hwg.emp.model.AskoffMD;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.AskoffService;
import com.hwg.emp.service.ServiceFactroy;

public class selectOwnAskoffController extends HttpServlet{
	AskoffService askoffservice=ServiceFactroy.getService(AskoffService.class);
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String dayStr=req.getParameter("days");
		Integer days=null;
		if (!ThisAppUtil.isNone(dayStr)) {
			days =Integer.parseInt(dayStr);
		}
		String reason=req.getParameter("reason");
		if (ThisAppUtil.isNone(reason)) {
			reason=null;
		}
	/*	String startdateStr=req.getParameter("startdate");
		Date startdate=null;
		if (ThisAppUtil.isNone(startdateStr)) {
			startdate=ThisAppUtil.parseDate(startdateStr, "yyyy-MM-dd");
		}
		String enddateStr=req.getParameter("startdate");
		Date enddate=null;
		if (ThisAppUtil.isNone(enddateStr)) {
			enddate=ThisAppUtil.parseDate(enddateStr, "yyyy-MM-dd");
		}
*/		
		
		//map.put("startdate", startdate);
		String pageNoStr=req.getParameter("page");
		if (pageNoStr==null) {
			pageNoStr="1";
		}
		Integer pageNo=null;
		if (!ThisAppUtil.isNone(pageNoStr)) {
			pageNo=Integer.parseInt(pageNoStr);
		}
		String pageSizeStr=req.getParameter("pageSize");
		Integer pageSize=null;
		if (pageSizeStr==null) {
			pageSizeStr="4";
		}
		if (!ThisAppUtil.isNone(pageSizeStr)) {
			pageSize=Integer.parseInt(pageSizeStr);
		}
		Map<String, Object> map=new HashMap<>();
		map.put("days", days);
		map.put("reason", reason);
		map.put("id", ((UserMD)req.getSession().getAttribute("USER")).getId());
		KeysQueryDto keys= new KeysQueryDto();
		keys.setKeys(map);
		keys.setPageNo(pageNo);
		keys.setPageSize(pageSize);
		List<AskoffMD> askoffMDs=new ArrayList<>();
		int total = 0;
		try {
			//总记录数
			total = askoffservice.selectAskoffByKey(keys, askoffMDs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		req.setAttribute("reason", reason);
		req.setAttribute("days", days);
		req.setAttribute("askoffMDs", askoffMDs);
		req.setAttribute("total", total);
		req.setAttribute("pageNo", pageNo);
		req.setAttribute("pageSize", pageSize);
		//起始记录数
		int totalNo=(pageNo-1)*pageSize;
		req.setAttribute("pageList", getPageList(total, pageNo, pageSize,days,reason));
		req.getRequestDispatcher("/pages/selectOwnAskoff.jsp").forward(req, resp);
		
	}
	private String getPageList(int total,int pageNo,int pageSize,Integer days,String reason){
		//算出多少页
		int totalPage = total%pageSize == 0?total/pageSize:total/pageSize+1;
		StringBuffer str=new StringBuffer();
		//首页
		str.append("<li><a href='selectOwnAskoff.do?page=1&days="+days+"&reason="+reason+"'>首页</a></li>");
		//上一页
		if (pageNo==1) {
			str.append("<li class='disabled'><a >上一页</a></li>");
		} else {
			str.append("<li><a href='selectOwnAskoff.do?page="+(pageNo-1)+"&days="+days+"&reason="+reason+"'>上一页</a></li>");
		}
		//循环的页码
		for (int i = pageNo-2; i <=pageNo+2; i++) {
			if (i<1||i>totalPage) {
				continue;
			}
			if (i==pageNo) {
				str.append("<li class='active'><a>"+i+"</a></li>");
			}else{
				str.append("<li><a href='selectOwnAskoff.do?page="+i+"&days="+days+"&reason="+reason+"'>"+i+"</a></li>");
			}
		}
		//下一页
		if (pageNo==totalPage) {
			str.append("<li class='disabled'><a>下一页</a></li>");
		} else {
			str.append("<li><a href='selectOwnAskoff.do?page="+(pageNo+1)+"&days="+days+"&reason="+reason+"'>下一页</a></li>");
			
		}
		//尾页
		str.append("<li><a href='selectOwnAskoff.do?page="+totalPage+"&days="+days+"&reason="+reason+"'>尾页</a></li>");
		return str.toString();
	}
		
		
		
	}

